package studio.banner.reference.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CheckUtil {
    /**
     * 有效参考年限，单位：年
     */
    private final Integer BOOKTERM = 5;
    private final Integer JOURNALTERM = 3;

    /**
     * 错误信息初始化
     */
    String wrongMessage;
    int wrongMessageNum = 1;

    /**
     * 文献类型标识符集合
     * group1-普通图书/会议录/汇编/学位论文
     * group2-期刊
     * group3-电子资源
     */
    String marketGroup1 = "(M|C|G|D)";
    String marketGroup2 = "J";

    /**
     * 格式检查，封装结果
     *
     * @param document
     * @return
     */
    public Map formatCheck(String document) {
        Map resultMap = new HashMap(10);
        boolean marketBool = true, punctuationBool = true, termBool = true;
        String docRegex = "", yearStr = "";
        String market = getMarket(document);
        marketBool = (market != null);
        if (marketBool) {
            docRegex = getRegex(market);
            Pattern pattern = Pattern.compile(docRegex);
            Matcher matcher = pattern.matcher(document);
            if (matcher.matches()) {
                yearStr = matcher.group(2);
                termBool = checkYear(yearStr, market);
            } else {
                punctuationBool = false;
            }
        }
        if (marketBool == false) {
            addToWrongMessage("文献类型标识符未找到或不符合规范!");
        }
        if (punctuationBool == false) {
            addToWrongMessage("文献格式错误，请检查是否缺漏项、标点符号是否为半角");
        }
        if (termBool == false) {
            addToWrongMessage("文献出版年不符合规范，期刊文献3年以内，其他文献5年以内");
        }
        if (wrongMessage == null) {
            resultMap.put("code", 0);
        } else {
            resultMap.put("code", 1);
            resultMap.put("message", wrongMessage);
        }
        return resultMap;
    }

    /**
     * 获取文献类型对应的正则表达式
     *
     * @param market
     * @return
     */
    public String getRegex(String market) {
        //正则表达式:普通图书、论文集、会议录、学位论文。
        String bookRegex = ".*\\..*\\[(M|C|G|D).*\\]\\..*\\:.*\\,(\\d+).*\\.";
        //正则表达式：期刊中的析出文献。
        String journalRegex = ".*\\..*\\[(J|J\\/OL)\\]\\..*\\,(\\d+)\\,.*\\.";
        //正则表达式：电子资源。
        String electronicDocRegex = ".*\\..*\\[(EB.*)\\]\\..*\\[(\\d+\\-\\d+\\-\\d+)\\]\\..*\\.";
        String docRegex;
        if (market.matches(marketGroup1)) {
            docRegex = bookRegex;
        } else if (market.matches(marketGroup2)) {
            docRegex = journalRegex;
        } else {
            docRegex = electronicDocRegex;
        }
        return docRegex;
    }

    /**
     * 出版年是否符合规范
     *
     * @param yearStr
     * @param market
     * @return
     */
    private boolean checkYear(String yearStr, String market) {
        int currentYear, docYear;
        currentYear = Calendar.getInstance().get(Calendar.YEAR);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //文献类型为期刊
        if (market.matches(marketGroup1) || market.matches(marketGroup2)) {
            docYear = Integer.parseInt(yearStr);
        } else {
            try {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(sdf.parse(yearStr));
                docYear = calendar.get(Calendar.YEAR);
            } catch (ParseException e) {
                e.printStackTrace();
            } finally {
                return false;
            }
        }
        if (market.matches(marketGroup2)) {
            return docYear <= currentYear && (currentYear - docYear) <= JOURNALTERM;
        } else {
            return docYear <= currentYear && (currentYear - docYear) <= BOOKTERM;
        }
    }

    /**
     * 判断页码是否正确
     *
     * @param pageNumStr
     * @return
     */
    private boolean checkPageNum(String pageNumStr) {
        String pageNumRegex = "(\\d+)\\-(\\d+)";
        Pattern pattern = Pattern.compile(pageNumRegex);
        Matcher matcher = pattern.matcher(pageNumStr);
        int front = Integer.parseInt(matcher.group(1));
        int back = Integer.parseInt(matcher.group(2));
        return back > front;
    }

    /**
     * 获取文献类型标识代码
     *
     * @param document
     * @return 标识代码
     */
    private String getMarket(String document) {
        String marketRegex = ".*\\..*\\[(M|C|G|D|J|EB).*\\]\\..*";
        Pattern pattern = Pattern.compile(marketRegex);
        Matcher matcher = pattern.matcher(document);
        if (matcher.find()) {
            return matcher.group(1);
        } else {
            return null;
        }
    }

    /**
     * 追加文献检查错误信息
     *
     * @param message
     */
    private void addToWrongMessage(String message) {
        wrongMessage = "[" + wrongMessageNum + "] " + message + "\n";
        wrongMessageNum++;
    }

}
